library PlayerData requires DatabaseLegacy, NOTDAWorldPhysics


	function RankSet takes integer p, boolean loading returns nothing
		local integer A = GetI("Rank",p)+1
		loop
			exitwhen udg_Experience[p]<GetI("XP",A)
			set A = A + 1
		endloop
		set A = A - 1
		if A>GetI("Rank",p) then
			call ShiftMoveSpeed(udg_Hero[p],-2*GetI("Rank",p))
			call ShiftMoveSpeed(udg_Hero[p],A*2)
			call SetI("Rank",p,A)
			
			if udg_CurrentWave>0 and loading == false  then
				call DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, GetPlayerName(ConvertedPlayer(p)) + " has been promoted to " + GetS("RankFN",A))
			endif

			if A>=13 then 
				call SetPlayerTechResearched(ConvertedPlayer(p),'Rhst',4)
			elseif A==12 then
				call SetPlayerTechResearched(ConvertedPlayer(p),'Rhst',3)
			elseif A>=10 then
				call SetPlayerTechResearched(ConvertedPlayer(p),'Rhst',2)
			elseif A>=8 then
				call SetPlayerTechResearched(ConvertedPlayer(p),'Rhst',1)
			endif
			if udg_MBcolors[p]!=3 and ((udg_MBcolors[p]!=0 and udg_CurrentWave>1) or udg_CurrentWave<=1) and GetPlayerSlotState(ConvertedPlayer(p))!=PLAYER_SLOT_STATE_LEFT then
				if A>=8 then
					set udg_MBcolors[p] = 1
				elseif A>=4 then
					set udg_MBcolors[p] = 2
				endif
			endif
			call SetPlayerName(ConvertedPlayer(p),GetS("RankN",A) + " " + GetS("PNames",p))
		endif
	endfunction

endlibrary
